﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoragePeriod.Common;
using StoragePeriod.Data;

namespace StoragePeriod.Web.SysManage
{
    public partial class AddRole : SecurityPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindData();
        }

        protected void gvRoleView_PreRender(object sender, EventArgs e)
        {
            // LinqUtility.LinqContext.Dispose();

            //if (gvRoleView.Rows.Count > 0)
            //{
            //    // 使用<TH>替换<TD>
            //    gvRoleView.UseAccessibleHeader = true;

            //    //This will add the <thead> and <tbody> elements
            //    //HeaderRow将被<thead>包裹，数据行将被<tbody>包裹
            //    gvRoleView.HeaderRow.TableSection = TableRowSection.TableHeader;

            //    // FooterRow将被<tfoot>包裹
            //    //gvRoleView.FooterRow.TableSection = TableRowSection.TableFooter;
            //}
        }

        private void BindData()
        {
          
                var res = from a in Role.All()
                          select new
                          {
                              角色编码 = a.RoleCode,
                              角色名称 = a.RoleName,
                              角色描述 = a.Memo
                          };
                gvRoleView.DataSource = res;
                gvRoleView.DataBind();
           
        }

        protected void btPost_Click(object sender, EventArgs e)
        {
            try
            {
                if (Role.Where("RoleCode==@0", tbRoleCode.Text.Trim()).Count() > 0)
                    throw new Exception();

                var newRole = new Role();
                newRole.RoleCode = tbRoleCode.Text;
                newRole.RoleName = tbRoleName.Text;
                newRole.Memo = tbRoleDesc.Text;
                newRole.RoleID = Guid.NewGuid().ToString();

                LinqUtility.Insert(newRole);

                BindData();
                Page.Alert(newRole.RoleName + ":添加成功");
            }
            catch (Exception ex)
            {
                Page.Alert("不能有重复权限码");
            }
        }

        protected void gvRoleView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LinqUtility.Delete(typeof(Role), "RoleCode == @0", e.Keys[0].EToString());
            Page.Alert("删除成功");

            BindData();
        }

        protected void gvRoleView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvRoleView.EditIndex = e.NewEditIndex;
            BindData();
        }

        protected void gvRoleView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                var updatePriv = new
                {
                    RoleName = e.NewValues[0],
                    Memo = e.NewValues[1]
                };
                Role.Update( updatePriv, "RoleCode == @0", e.Keys[0].EToString());
                gvRoleView.EditIndex = -1;

                BindData();
                Page.Alert("更新成功");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void gvRoleView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvRoleView.EditIndex = -1;

            BindData();
        }

        protected void gvRoleprivilegeView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            BindData();
        }
    }
}